using Ivony.Html;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;


namespace DownLoadUNPKG
{
    class Program
    {
        static Ivony.Html.Parser.JumonyParser jumony;
        static IHtmlDocument doc;
       // static string url = "https://unpkg.com/browse/element-ui@2.13.0/";
        static string url = "";

        static WebClient web = new WebClient();

        static string baseDir = Environment.CurrentDirectory + "\\Data\\";


        static List<Model> list = new List<Model>();

        static void Main(string[] args)
        {
            jumony = new Ivony.Html.Parser.JumonyParser();
            web.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36");

            Console.WriteLine("说明：");
            Console.WriteLine("例如：https://unpkg.com/browse/element-ui@2.13.0/");
            Console.WriteLine("输出文件保存在根目录Data中：");
            Console.WriteLine("请输入地址：");
            url = Console.ReadLine();

            readFile(url, baseDir);

            Parallel.ForEach(list, (item, LoopState) =>
            {
                DownloadFile(item.url, item.Address);
                Console.WriteLine("正在下载"+ item.url);
            });

            Console.WriteLine("下载完成");
            Console.ReadKey();
        }

        static void readFile(string myUrl, string fileAddress)
        {
         
            byte[] downData;
            try
            {
                downData = web.DownloadData(myUrl);
            }
            catch
            {
                return;
            }
            string html = Encoding.UTF8.GetString(downData);
            doc = new Ivony.Html.Parser.JumonyParser().Parse(html);
            var tds = doc.Find("td a");
            foreach (var item in tds)
            {
                var value = item.Attribute("href").Value();
                if (value == "../")
                    continue;
                var dd = myUrl + value;
                //list.Add(dd);
                Console.WriteLine(dd);
                string endValue = value.Substring(value.Length - 1, 1);
                if (endValue == "/")
                {
                    string address = fileAddress + value.TrimEnd('/') + "\\";
                    if (!Directory.Exists(address))//如果不存在就创建 dir 文件夹  
                        Directory.CreateDirectory(address);
                    
                    readFile(dd, address);
                }
                else
                {
                    string address = fileAddress + value;
                    try
                    {
                        //web.DownloadFile(new Uri(dd), address,);
                        dd = dd.Replace("/browse", "");
                        list.Add(new Model()
                        {
                            url = dd,
                            Address = address
                        });
                      //  DownloadFile(dd, address);
                    }
                    catch 
                    {
                    }
                    
                }
            }
        }


        ///<summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL">下载文件地址</param>
        /// <param name="Filename">下载后另存为（全路径）</param>
        static bool DownloadFile(string URL, string filename)
        {
            try
            {
                HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                Stream st = myrp.GetResponseStream();
                Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                myrp.Close();
                Myrq.Abort();
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }
    }
}
